{"componentChunkName":"component---src-templates-blog-post-js","path":"/Setting-up-page-structure-for-a-multilingual-website-in-Gatsby-and-React/","result":{"data":{"site":{"siteMetadata":{"title":"Aleks Popovic - personal website and blog"}},"markdownRemark":{"id":"5a07838d-4259-5b77-a5a6-8037ca8edd0f","excerpt":"Before going ahead and making a multilingual website I would advise you to think about a few things: Do you even need one? Are you prepared to make multilingual…","html":"<p>Before going ahead and making a multilingual website I would advise you to think about a few things:</p>\n<ul>\n<li>Do you even need one?</li>\n<li>Are you prepared to make multilingual content on your own?</li>\n<li>What is the best way to handle various translations?</li>\n</ul>\n<p>The first two points really matter only if you are making a personal project. If you were contracted to make a multilingual site for someone else then they are the ones who need to think about it.</p>\n<p>I was recently hired to make a multilingual website with at least three different language options and point #3 was something I had to think about for a bit. Here is how I solved it with Gatsby and React, and maybe it can ispire you to make a similar solution for your own project.</p>\n<h2>Page setup for Gatsby</h2>\n<p>There is a very handy Gatsby plugin for setting up different multilingual pages called <a href=\"https://github.com/angeloocana/gatsby-plugin-i18n\">gatsby-plugin-i18n</a>. It basically works by looking at your page files’ names and assigning a route to them based on a pattern.</p>\n<p>Let’s say you have a page called contact.js which has a path that looks like this: src/pages/about.js . To add different language versions of that page you need to change the file name by adding the language code to it.</p>\n<p>For example, changing the file path to src/pages/about.en.js will change the final page url from yourwebsite.com/about to yourwebsite.com/en/about . Now to add the same page for a different language, let’s say German, you would make another file with a path like this: src/pages/about.de.js, which would make the final url look like this: yourwebsite.com/de/about .</p>\n<p>As you can already tell, this is pretty useful, and easy to setup. Of course - the plugin doesn’t actually translate the page content. It “just” automatically sets up all of your page routing based on your page names, which is really neat.</p>\n<h2>Installation</h2>\n<p>To install the plugin you can run</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"language-jsx\"><code class=\"language-jsx\">npm install gatsby<span class=\"token operator\">-</span>plugin<span class=\"token operator\">-</span>i18n</code></pre></div>\n<p>or</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"language-jsx\"><code class=\"language-jsx\">yarn add gatsby<span class=\"token operator\">-</span>plugin<span class=\"token operator\">-</span>i18n</code></pre></div>\n<h2>Configuration</h2>\n<p>For the most basic setup you will need to edit your gatsby-config.js file and add this to it:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"language-jsx\"><code class=\"language-jsx\">plugins<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n  <span class=\"token punctuation\">{</span>\n    resolve<span class=\"token operator\">:</span> <span class=\"token string\">\"gatsby-plugin-i18n\"</span><span class=\"token punctuation\">,</span>\n    options<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n      langKeyDefault<span class=\"token operator\">:</span> <span class=\"token string\">\"en\"</span><span class=\"token punctuation\">,</span>\n      useLangKeyLayout<span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">]</span></code></pre></div>\n<p>You can fine-tune your settings by editing the options object. I went with these settings in the end, but you can find all possible options on the plugin’s <a href=\"https://github.com/angeloocana/gatsby-plugin-i18n#all-options\">repo page</a> and play around with them.</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"language-jsx\"><code class=\"language-jsx\">plugins<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n  <span class=\"token punctuation\">{</span>\n    resolve<span class=\"token operator\">:</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token string\">gatsby-plugin-i18n</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">,</span>\n    options<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n      langKeyDefault<span class=\"token operator\">:</span> <span class=\"token string\">\"en\"</span><span class=\"token punctuation\">,</span>\n<span class=\"gatsby-highlight-code-line\">      langKeyForNull<span class=\"token operator\">:</span> <span class=\"token string\">\"en\"</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// Sets language key to \"en\" if it's not provided</span></span><span class=\"gatsby-highlight-code-line\">      prefixDefault<span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// Removes url prefix for your default language</span></span>      useLangKeyLayout<span class=\"token operator\">:</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">]</span></code></pre></div>\n<p>The only thing left is to actually add your pages and fill them with your multilingual content.</p>\n<h2>Non-Gatsby approach</h2>\n<p>If you aren’t using Gatsby to build your website there are pure React or even pure JavaScript solutions to your problem. Actually, if you are using pretty much anything else you can head over to <a href=\"https://www.i18next.com/overview/supported-frameworks\">i18next</a> and check out their list of supported frameworks with full documentation and examples.</p>\n<h2>What’s next?</h2>\n<p>Our pages our now configured, but we don’t heave an easy way of moving between them. In the <a href=\"/Building-a-language-switch-component-in-React/\">next article</a> we will go over the process of making a simple React component for selecting your preferred language, so you don’t have to manually enter your page address if you want to switch to a different language.</p>\n<p>We will cover a simpler solution for running a bilingual website and also a bit more complex one that supports more than two languages.</p>\n<hr>\n<div class=\"sectionHighlight\">If you have any questions or comments you can reach out to me on <a href=\"https://twitter.com/alekswritescode\" target=\"_blank\" rel=\"noopener noreferrer\">Twitter</a> and <a href=\"https://www.instagram.com/aleks.popovic/\" target=\"_blank\" rel=\"noopener noreferrer\">Instagram</a>, where I also post interesting code tidbits and designs.\n<p>I also have a <a href=\"https://www.youtube.com/AleksPopovic\" target=\"_blank\" rel=\"noopener noreferrer\">YouTube channel</a> where I regularly upload React and web dev tutorials, so if that’s your cup of tea feel free to support me by subscribing.</div></p>","frontmatter":{"title":"Setting up page structure for a multilingual website","series":"How to Build a Multilingual Website with Gatsby and React","seriesLink":"multilingual","date":"August 05, 2020","description":"Before going ahead and making a multilingual website I would advise you to think about a few things. Do you even need one? Are you prepared to make multilingual content on your own? What is the best way to handle various translations? The first two points really matter only if you are making a personal project. If you were contracted to make a multilingual site for someone else then they are the ones who need to think about it.","tags":["gatsby","web","react","multilingual"],"featuredImage":{"childImageSharp":{"fluid":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACQklEQVQozx2S20/SYRjH+T+8aqv7buqmtWVunTbrQi0tEeWMCESgKSAnQdkPEVOzw4aWHW1WNoepLduymjmjUkRFREytbHNrXrSu+vTK1bNne9/n+Xw/7yvrNG1x07xDuzdFk3eSgGKJkDZLSL1MSJNG0n/DfPoNZQcfYjrzlqBmDV9NmlbVCr7KOXQnX+OSpwiIvql8FllP3RbdzmVs01GcmXac1+J0KTfxVC/Qpk7TUDLDqX0dFBVIFBaEsZckCKoX8VenaNFkaZbP0aJeFWczNJ6fQRbRruGtn8aRDGPbCBO76+Nvo4fbign6PZNsDRqYdBm5Z7EzbDUze93G+oCLdMzDQiyIpE7iUYgFNYs0lieQ+ZUZgTyP6c5TGiL32enogHgXc8ZOHrsnWIqoGKs3EdPZGbHWkZBMzHf7SPU08SXqI2IQcdW5fGS/KoNM0ueIGL/TZ0mSaa7iT5uczatexrSDPHLFxSUdU5KRuMPCVKuS544oDRUZHJeW8AqykHY17zUgYge1OWRd5g1Chl1Gnb3w7DD/hot44XiAU7HLE/cIize8jDfaGDJfZshopd/Sh774K7Vn34vBSQKaHD7liqhruOVJ4bB2HcmwTd+VD/zuPURGKhWvnhCSswSFDqlqDv/xAdT7JcoO3EJe9IqLR19y4cgIteemcQuyPW2tunXspR/3CH/Sadqm27zJgO0d0bpfhA0/COvFIl0Wqfoz7RXvaCkexXpiFI34OpXHxqkpHMNa+ilP5let5qtLscx/wqWxW+oo+ccAAAAASUVORK5CYII=","aspectRatio":2.6666666666666665,"src":"/static/62314cdf6f6a00bf234dd2a5e569228a/0e6e2/structure.png","srcSet":"/static/62314cdf6f6a00bf234dd2a5e569228a/b460a/structure.png 480w,\n/static/62314cdf6f6a00bf234dd2a5e569228a/6e898/structure.png 960w,\n/static/62314cdf6f6a00bf234dd2a5e569228a/0e6e2/structure.png 1920w","srcWebp":"/static/62314cdf6f6a00bf234dd2a5e569228a/30cf3/structure.webp","srcSetWebp":"/static/62314cdf6f6a00bf234dd2a5e569228a/35871/structure.webp 480w,\n/static/62314cdf6f6a00bf234dd2a5e569228a/fcc10/structure.webp 960w,\n/static/62314cdf6f6a00bf234dd2a5e569228a/30cf3/structure.webp 1920w","sizes":"(max-width: 1920px) 100vw, 1920px"}}}}}},"pageContext":{"slug":"/Setting-up-page-structure-for-a-multilingual-website-in-Gatsby-and-React/","previous":{"excerpt":"I started working on this website in January, but things didn’t really work out the way I wanted to. It’s tough to have everything go…","fields":{"slug":"/How-to-finish-your-projects/"},"frontmatter":{"date":"June 01, 2020","title":"How to finish your projects","series":null,"description":"I started working on this website in January, but things didn't really work out the way I wanted to. It's tough to have everything go according to plan when you don't have a plan in the first place. I knew I wanted to redesign my old website which was was just one static page with a list of my projects. At the time I made it that was enough, but I wanted to extend it at some point to be a place where I can do and show more things, not necessarily related to web development. I guess this is that point.","tags":["web","productivity"],"featuredImage":{"id":"b305873e-00e2-563c-8a4e-0e7d7afaaa1a","publicURL":"/static/004be15f86e180f340923c653a6377a3/finish.png","relativeDirectory":"How-to-finish-your-projects","relativePath":"How-to-finish-your-projects/finish.png"}}},"next":{"excerpt":"You have all of your multilingual pages set up, but now you need to give your users a way to access those pages. There are multiple ways…","fields":{"slug":"/Building-a-language-switch-component-in-React/"},"frontmatter":{"date":"August 14, 2020","title":"Building a language switch component in React","series":"How to Build a Multilingual Website with Gatsby and React","description":"You have all of your multilingual pages set up, but now you need to give your users a way to access those pages. There are multiple ways this can be done. The most common solution is adding a language selection tool in the form of multiple links or by using a drop-down list. In my case - I had to implement three different languages, so I went with the buttons approach.","tags":["web","react","multilingual"],"featuredImage":{"id":"797519ed-9107-5830-a615-f6b80bf76978","publicURL":"/static/b4540dc780b9271335e7b48d0014a453/switch.png","relativeDirectory":"Building-a-language-switch-component-in-React","relativePath":"Building-a-language-switch-component-in-React/switch.png"}}}}}}